<template>
    <div class="user-profile-container">
        <!-- 返回按钮 -->
        <router-link to="/home" class="back-button">
            <img src="@/assets/images/home/backright.png" alt="返回图标" class="return-image" />
        </router-link>

        <!-- 用户信息卡片 -->
        <div class="user-card">
            <div class="avatar">
                <img :src="userAvatar" alt="用户头像" />
            </div>
            <div class="user-info">
                <span class="nickname">{{ userInfo.nickname }}</span>
                <span class="signature">{{ userInfo.email }}</span>
            </div>
        </div>

        <!-- 功能列表 -->
        <div class="function-list">
            <div class="function-group">
                <div class="function-item" v-for="(item, index) in functionGroup1" :key="index" @click="item.action">
                    <div class="function-icon">{{ item.icon }}</div>
                    <span>{{ item.title }}</span>
                </div>
            </div>

            <div class="function-group">
                <div class="function-item" v-for="(item, index) in functionGroup2" :key="index" @click="item.action">
                    <div class="function-icon">{{ item.icon }}</div>
                    <span>{{ item.title }}</span>
                </div>
            </div>
        </div>

        <!-- 版权信息 -->
        <div class="copyright">
            &copy; 2025 灵猫智问开发团队，版权所有.
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';

const userStore = useUserStore();
const router = useRouter();

const userAvatar = ref(userStore.userInfo.userPic ? userStore.userInfo.userPic : 'src/assets/images/home/catAvatar.png');
const userInfo = ref({
    nickname: userStore.userInfo.nickname,
    email: userStore.userInfo.email
});

// 先定义 handleLogout 函数
const handleLogout = () => {
    userStore.clearUserInfo();
    router.push('/login');
};

// 然后再使用它
const functionGroup1 = [
    { title: '推文点赞', icon: '📝', action: () => router.push('/articleCollection') },
    { title: '我的收藏', icon: '⭐', action: () => router.push('/tweetLike') },
    { title: '文章浏览记录', icon: '🕒', action: () => router.push('/articleHistory') }
];

const functionGroup2 = [
    { title: '购物车', icon: '🛒', action: () => router.push(`/petStore/shoppingCart/${userStore.userInfo.id}`) },
    { title: '我的订单', icon: '📦', action: () => router.push(`/order/myOrders/${userStore.userInfo.id}`) },
    { title: '退出登录', icon: '🚪', action: handleLogout }
];
</script>

<style scoped lang="scss">
.user-profile-container {
    min-height: 100vh;
    background: linear-gradient(135deg, #8B5CF6, #6366F1);
    padding: 20px;
    color: #fff;
    position: relative;
}

.back-button {
    position: absolute;
    top: 20px;
    right: 20px;
    z-index: 10;

    .return-image {
        width: 24px;
        height: 24px;
        filter: brightness(0) invert(1);
    }
}

.user-card {
    margin-top: 20px;
    margin-left: 10px;
    display: flex;
    align-items: center;
    padding: 15px;

    .avatar {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        border: 3px solid rgba(255, 255, 255, 0.3);
        overflow: hidden;

        img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
    }

    .user-info {
        margin-left: 15px;

        .nickname {
            font-size: 20px;
            font-weight: 600;
            display: block;
            margin-bottom: 5px;
        }

        .signature {
            font-size: 14px;
            opacity: 0.8;
        }
    }
}

.function-list {
    margin-top: 30px;

    .function-group {
        background: rgba(255, 255, 255, 0.1);
        backdrop-filter: blur(10px);
        border-radius: 16px;
        padding: 15px;
        margin-bottom: 15px;

        .function-item {
            display: flex;
            align-items: center;
            padding: 15px;
            border-radius: 12px;
            transition: all 0.3s ease;
            background: rgba(255, 255, 255, 0.08);
            margin-bottom: 8px;
            border: 1px solid rgba(255, 255, 255, 0.1);

            &:last-child {
                margin-bottom: 0;
            }

            &:hover {
                background: rgba(255, 255, 255, 0.15);
                transform: translateX(5px);
                border-color: rgba(255, 255, 255, 0.2);
            }

            &:active {
                background: rgba(255, 255, 255, 0.2);
                transform: translateX(3px) scale(0.98);
            }

            .function-icon {
                width: 24px;
                height: 24px;
                margin-right: 15px;
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 18px;
                background: rgba(255, 255, 255, 0.1);
                padding: 8px;
                border-radius: 8px;
            }

            span {
                font-size: 16px;
                font-weight: 500;
                letter-spacing: 0.5px;
            }
        }
    }
}

.copyright {
    position: fixed;
    bottom: 20px;
    left: 0;
    right: 0;
    text-align: center;
    font-size: 12px;
    color: rgba(255, 255, 255, 0.7);
    padding: 10px;
}
</style>
